package com.andova.toolkit.adapter.image;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import androidx.collection.ArrayMap;

import com.andova.common.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Zsago on 2017/5/17.
 * <p>后面抽空，把UI层分离出去</p>
 *
 * @author kzaxil
 */
public abstract class SimpleDeletableImagesAdapter<T extends SimpleDeletableImagesAdapter.PhotoBean> extends BaseAdapter {
    private Context mContext;
    private int mMaxImages = 10;
    private Map<Integer, View> mMap = new ArrayMap<>(mMaxImages);
    private List<T> mPhotosBeanList = new ArrayList<>(mMaxImages);
    private List<View.OnClickListener> mListeners = new ArrayList<>(mMaxImages);

    public SimpleDeletableImagesAdapter(Context context) {
        mContext = context;
    }

    public SimpleDeletableImagesAdapter(Context context, int maxImages) {
        mContext = context;
        this.mMaxImages = maxImages;
    }

    public void add(T bean) {
        if (mPhotosBeanList.size() >= mMaxImages) {
            mPhotosBeanList.set(mMaxImages - 1, bean);
        } else {
            mPhotosBeanList.add(bean);
        }
        if (mListener != null) mListener.changed(mMaxImages, getImageSize());
        notifyDataSetChanged();
    }

    public void remove(int position) {
        mPhotosBeanList.remove(position);
        if (mListener != null) mListener.changed(mMaxImages, getImageSize());
        notifyDataSetChanged();
    }

    public List<T> list() {
        for (int i = 0; i < mPhotosBeanList.size(); i++) {
            mPhotosBeanList.get(i).order_number = i;
        }
        return mPhotosBeanList;
    }

    public void clear() {
        mPhotosBeanList.clear();
        notifyDataSetChanged();
    }

    public boolean max() {
        return mPhotosBeanList.size() >= mMaxImages;
    }

    public String getImageUrl(int position) {
        if (position < 0 || mPhotosBeanList == null || position > mPhotosBeanList.size() - 1)
            return null;
        return mPhotosBeanList.get(position).photo_url;
    }

    public int getImageSize() {
        return mPhotosBeanList.size();
    }

    @Override
    public int getCount() {
        return mPhotosBeanList.size() < mMaxImages ? mPhotosBeanList.size() + 1 : mMaxImages;
    }

    @Override
    public Object getItem(int position) {
        return position < mPhotosBeanList.size() ? mPhotosBeanList.get(position) : position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        SimpleDeletableImagesViewHolder vh;
        if (!mMap.containsKey(position)
                || mMap.get(position) == null
                || mMap.get(position) == null) {
            vh = new SimpleDeletableImagesViewHolder();
            convertView = vh.getView(mContext, parent);
            convertView.setTag(vh);
            mMap.put(position, convertView);
            mListeners.add(position, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    remove(position);
                    mMap.remove(position);
                    mListeners.remove(position);
                    notifyDataSetChanged();
                }
            });
        } else {
            convertView = mMap.get(position);
            vh = (SimpleDeletableImagesViewHolder) convertView.getTag();
        }
        display(vh, position);
        return convertView;
    }

    private void display(SimpleDeletableImagesViewHolder viewHolder, final int position) {
        if (position == getCount() - 1 && mPhotosBeanList.size() < mMaxImages) {
            viewHolder.ivDelete.setVisibility(View.GONE);
            viewHolder.ivImage.setImageResource(R.mipmap.adroit_ic_take_photo);
        } else if (position < mPhotosBeanList.size()) {
            viewHolder.ivDelete.setVisibility(View.VISIBLE);
            displayImage(mPhotosBeanList.get(position), viewHolder.ivImage);
            viewHolder.ivDelete.setOnClickListener(mListeners.get(position));
        }
    }

    private OnImageCountChangedListener mListener;

    public interface OnImageCountChangedListener {
        void changed(int max, int current);
    }

    public void setOnImageCountChangedListener(OnImageCountChangedListener listener) {
        mListener = listener;
    }

    public static class PhotoBean {
        public int order_number;
        public String photo_url;
    }

    protected abstract void displayImage(PhotoBean photoBean, ImageView imageView);
}